package com.coco.service.impl;

import com.coco.config.JwtProperties;
import com.coco.entity.UserInfo;
import com.coco.model.CollectionStatus;
import com.coco.model.RecordInfo;
import com.coco.model.RecordModel;
import com.coco.proxy.ManagerFeign;
import com.coco.service.CommonService;
import com.coco.utils.CookieUtils;
import com.coco.utils.JwtUtils;
import org.bouncycastle.asn1.cmp.OOBCertHash;
import org.bouncycastle.jcajce.provider.symmetric.DES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.DirectoryNotEmptyException;
import java.util.List;

/**
 * @author GHOSTLaycoo
 * @date 2020/4/28 - 11:11
 */
@EnableConfigurationProperties(JwtProperties.class)
@Service
public class CommonServiceImpl implements CommonService{

    @Autowired
    private ManagerFeign managerFeign;

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public int saveHistory(CollectionStatus collectionStatus, HttpServletRequest request) {
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            //加注释
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            RecordModel recordModel = new RecordModel();
            recordModel.setVideoId(collectionStatus.getVideoId());
            recordModel.setUserId(infoFromToken.getId());
            ResponseEntity<Integer> save = managerFeign.save(recordModel);
            return save.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteHistory(HttpServletRequest request) {
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            ResponseEntity<Integer> delete = managerFeign.delete(infoFromToken.getId());
            return delete.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<RecordInfo> getTodeyHistory(HttpServletRequest request) {
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            ResponseEntity<List<RecordInfo>> listResponseEntity = managerFeign.searchRecord(infoFromToken.getId());
            if(!CollectionUtils.isEmpty(listResponseEntity.getBody())){
                return listResponseEntity.getBody();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
